﻿using System.Collections.Generic;
using System.Linq;

namespace EnglishWords.Logic
{
    public static class DbManager
    {
        private static EnglishWordsDBEntities context = new EnglishWordsDBEntities();

        public static bool CheckUser(string username, string password)
        {
            var correctPassword = (from p in context.Users
                                   where p.Username == username
                                   select p.Password);

            if (correctPassword.Count() != 0)
            {
                string pass = correctPassword.First().ToString();
                if (password == pass)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public static List<User> GetAllUsers()
        {
            var users = context.Users.ToList();

            return users;
        }

        public static List<string> GetAllCategories()
        {
            var categories = context.UserCategories.Select(c => c.UserCategoryName).ToList();

            return categories;
        }

        public static void AddUser(string userName, string password, int categoryId)
        {
            context.Users.AddObject(new User { Username = userName, Password = password, UserCategoryId = categoryId });
            context.SaveChanges();
        }

        public static string GetIdByCategory(string category)
        {
            var categoryId =
                context.UserCategories.FirstOrDefault(c => c.UserCategoryName == category).UserCategoryId.ToString();
            return categoryId;
        }
    }
}
